package com.beagle.njddapp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpFragment;
import com.beagle.njddapp.bean.owner.DIrverOrderBean;
import com.beagle.njddapp.event.JieDanEvent;
import com.beagle.njddapp.ui.activity.MainActivity;
import com.beagle.njddapp.ui.activity.dirver.DirverOrderDetailActivity;
import com.beagle.njddapp.ui.adapter.DirverHomeListAdapter;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;
import com.beagle.njddapp.ui.presenter.DirverHomePresenter;
import com.beagle.njddapp.ui.view.DirverHomeView;
import com.blankj.utilcode.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author hxx
 * @date 2021-03-15 19:29
 * 农机驾驶员 首页 接单列表
 */
public class DirverHomeFragment extends BaseMvpFragment<DirverHomeView, DirverHomePresenter> implements DirverHomeView, DirverHomeListAdapter.DirverHomeListJieDanCallback, RecyclerViewItemClickLisenter<DIrverOrderBean> {

    @BindView(R.id.common_recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.ll_empty)
    LinearLayout llEmpty;
    @BindView(R.id.smartrefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private List<DIrverOrderBean> dirverHomeBeanList;
    private DirverHomeListAdapter dirverHomeListAdapter;
    private int type=-1;
    private boolean finish;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments()!=null){
            Bundle bundle=getArguments();
            type=bundle.getInt("type",-1);
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public void init() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        dirverHomeBeanList=new ArrayList<>();
        dirverHomeListAdapter=new DirverHomeListAdapter(dirverHomeBeanList,getContext());
        dirverHomeListAdapter.setDirverHomeListJieDanCallback(this);
        dirverHomeListAdapter.setOnItemClickLisenter(this);
        recyclerView.setAdapter(dirverHomeListAdapter);
        smartRefreshLayout.setEnableRefresh(true);
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                if (type==1){
                    presenter.getList();
                }else {
                   presenter.getDirverOrder(1000,1);
                }
            }
        });
        if (type==1){
            presenter.getList();
        }else {
            presenter.getDirverOrder(1000,1);

        }
    }
    @Override
    public DirverHomeView getMvpView() {
        return this;
    }

    @Override
    public DirverHomePresenter createPresenter() {
        return new DirverHomePresenter();
    }

    @Override
    public void onError(String message) {
        smartRefreshLayout.finishRefresh();
        if (!TextUtils.isEmpty(message)){
            ToastUtils.showShort(message);
        }
    }

    @Override
    public void showLIst(List<DIrverOrderBean> results) {
        smartRefreshLayout.finishRefresh();
        if (results==null||results.size()<1){
            llEmpty.setVisibility(View.VISIBLE);
        }else {
            llEmpty.setVisibility(View.GONE);
        }
        dirverHomeBeanList.clear();
        for (DIrverOrderBean dIrverOrderBean:results){
            if (dIrverOrderBean.getOrderStatus()==6){
                continue;
            }
            dirverHomeBeanList.add(dIrverOrderBean);
        }
        dirverHomeListAdapter.notifyDataSetChanged();
    }

    @Override
    public void updateSuccess() {
        if (finish){
            ToastUtils.showShort("确认成功");
            llEmpty.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (getActivity()!=null &&getActivity() instanceof MainActivity){
                        ((MainActivity)getActivity()).toDirverMap();
                    }
                }
            },1000);
            return;
        }
        ToastUtils.showShort("接单成功");
        type=1;
        presenter.getList();
    }

    @Override
    public void jiedan(DIrverOrderBean dirverHomeBean) {
        if (1==dirverHomeBean.getOrderStatus()){
            dirverHomeBean.setOrderStatus(2);
            presenter.updateFindDriverOrder(dirverHomeBean);
        }else if (3==dirverHomeBean.getOrderStatus()){
            dirverHomeBean.setOrderStatus(4);
            finish=true;
            presenter.updateFindDriverOrder(dirverHomeBean);
        }
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onItemClickLisenter(DIrverOrderBean dirverHomeBean) {
        startActivity(new Intent(getContext(), DirverOrderDetailActivity.class).putExtra("data",dirverHomeBean).putExtra("type",1));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe
    public void onEvent(JieDanEvent jieDanEvent){
        type=1;
        presenter.getList();
    }
}
